# Read the data file and run analyses.

setwd("F:/Experiment Data/Complex Contagion/ComplexContagionDatasets/")
exp1 = read.csv("Exp1_innocuous_questions.csv")

# Code dependent variables.
# Code Self, Schis and Other columns.
exp1$Self = 0
exp1[exp1$userChoice == "Keep_10_cents_and_Do_Not_Donate", c("Self")] = 1
exp1$Schis = 0
exp1[exp1$userChoice == "Schistosomiasis_Control_Initiative", c("Schis")] = 1
exp1$Other = 0
exp1[exp1$Self == 0 & exp1$Schis == 0, c("Other")] = 1

# Code AnyCharity column.
exp1$AnyCharity = 0
exp1[exp1$userChoice != "Keep_10_cents_and_Do_Not_Donate", c("AnyCharity")] = 1

# Code independent variables.
# Code Tie Strength column.
exp1$TieStrength = "High"
exp1[grepl("^AllLo__", exp1$conditionName), c("TieStrength")] = "Low"
#exp1[, c("conditionName", "TieStrength")]

# Code SelfNeighbors, SchisNeighbors and OtherNeighbors columns.
exp1$SelfNeighbors = 0
exp1[grepl("Self_1", exp1$conditionName), c("SelfNeighbors")] = 1
exp1[grepl("Self_2", exp1$conditionName), c("SelfNeighbors")] = 2
exp1[grepl("Self_3", exp1$conditionName), c("SelfNeighbors")] = 3

exp1$SchisNeighbors = 0
exp1[grepl("Schis_1", exp1$conditionName), c("SchisNeighbors")] = 1
exp1[grepl("Schis_2", exp1$conditionName), c("SchisNeighbors")] = 2
exp1[grepl("Schis_3", exp1$conditionName), c("SchisNeighbors")] = 3

exp1$OtherNeighbors = 0
exp1[grepl("Rand_1", exp1$conditionName), c("OtherNeighbors")] = 1
exp1[grepl("Rand_2", exp1$conditionName), c("OtherNeighbors")] = 2
exp1[grepl("Rand_3", exp1$conditionName), c("OtherNeighbors")] = 3

#exp1[, c("conditionName", "TieStrength", "SelfNeighbors", "SchisNeighbors", "OtherNeighbors")]


# Influence curves.  First split by tie strength, then together.
# UserChoice is Schis by Schis neighbor count.  Split by Tie Strength.
aggregate(Schis ~ TieStrength + SchisNeighbors, exp1, mean)
aggregate(Schis ~ SchisNeighbors, exp1, mean)

# Means will give us the proportions, but let's also get the raw numerator and denominator.
exp1$countMe = 1
aggregate(Schis ~ TieStrength + SchisNeighbors, exp1, sum)
aggregate(countMe ~ TieStrength + SchisNeighbors, exp1, sum)
table(exp1$SchisNeighbors)

# Fit a line and then predict values.
schisDataToFit = as.data.frame(aggregate(Schis ~ SchisNeighbors, exp1, mean))
linearModel = lm(Schis ~ SchisNeighbors, data=schisDataToFit)
summary(linearModel)
xValues = c(0,1,2,3)
predict(linearModel, list(SchisNeighbors=xValues))


